package com.mk.service.platform;

import act.validation.NotBlank;
import com.github.d925529.apidoc.ApiHttpMethod;
import com.github.d925529.apidoc.annotation.*;
import com.mk.properties.AppConfig;
import com.mk.service.BasePaasService;
import org.apache.bval.constraints.NotEmpty;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.With;
import org.osgl.mvc.result.Result;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@Api(group = "pl", title = "通用系统服务")
@SuppressWarnings("unused")
public class PaasService extends BasePlatformService {
    @Inject
    private BasePaasService basePaasService;

    @GetAction("getWxConfig.json")
    @ApiMethod(title = "获取公众号配置", path = "getWxConfig.json", method = ApiHttpMethod.GET, description = "获取公众号配置")
    @ApiReturn(description = "微信配置map", type = Map.class, maps = {
        @ApiMap(name = "appid", title = "appid", type = String.class),
        @ApiMap(name = "appsec", title = "appsec", type = String.class)
    })
    public Map<String, Object> getWxConfig() {
        Map<String, Object> map = new HashMap<>();
        map.put("appid", AppConfig.weixin.appid.get());
        map.put("appsecret", AppConfig.weixin.appsec.get());
        return map;
    }

    @GetAction("GlobalValues.json")
    @ApiMethod(title = "获取系统枚举配置", path = "/GlobalValues.json", method = ApiHttpMethod.GET, description = {
        "此方法会缓存10小时"
    })
    @ApiReturn(description = "系统枚举配置", type = Map.class, maps = {
        @ApiMap(name = "GlobalMap", title = "key->value格式的枚举值", type = String.class),
        @ApiMap(name = "GlobalArray", title = "数组格式的枚举值", type = String.class)
    })
    public Map getConfigJson() {
        return basePaasService.getConfigJson();
    }

    @GetAction("query.json")
    @With(ILogin.class)
    @ApiMethod(title = "通用查询", path = "query.json", method = ApiHttpMethod.GET, description = {
        "为mk-table提供的通用查询",
        "参数仅列出公用参数，其他每个查询独有的查询条件不列出"
    })
    @ApiParam(name = "sqlCode", title = "对应的查询语句", type = String.class, description = {
        "一般格式是sql目录中的文件名.sql语句",
        "例如cpuser.md中的select_list,就是cpuser.select_list"
    })
    @ApiParam(name = "currentPage", title = "当前页", type = int.class, required = false)
    @ApiParam(name = "pageSize", title = "每页记录数", type = int.class, required = false)
    @ApiParam(name = "sortName", title = "排序列", type = String.class, required = false, description = {
        "设置el-table-column的sort属性为true即可按此列排序"
    })
    @ApiParam(name = "sortType", title = "排序方式", type = String.class, required = false, description = {
        "默认asc"
    })
    @ApiReturn(description = "查询结果", type = Map.class, maps = {
        @ApiMap(name = "list", title = "结果", type = String.class, description = {
            "查询结果集，是json对象"
        }),
        @ApiMap(name = "totalRow", title = "总记录数", type = int.class)
    })
    public PageQuery<Map> query(@NotBlank String sqlCode,
                                String currentPage,
                                String pageSize,
                                String sortName,
                                String sortType) {
        return basePaasService.query(sqlCode, currentPage, pageSize, sortName, sortType);
    }

    @GetAction("validateCode.jpegx")
    @ApiMethod(title = "获取图形验证码", path = "validateCode.jpegx", method = ApiHttpMethod.GET, description = {
        "生成图形验证码,30分钟有效",
        "客户端调用过后",
        "后台通过cached(CacheKey.PICCODE.value()) 获取缓存的值"
    })
    public Result createPicCode(String index) {
        return basePaasService.createPicCode(index);
    }


    @GetAction("getSmsCode.sms")
    @ApiMethod(title = "获取短信验证码", path = "getSmsCode.sms", method = ApiHttpMethod.GET, description = {
        "生成短信验证码,30分钟有效",
        "客户端调用过后",
        "后台通过cached(CacheKey.SMS_CODE.value()) 获取缓存的值",
        ""
    })
    @ApiParam(name = "phone", title = "手机号码", type = String.class)
    public String getSmsCode(String phone) {
        return basePaasService.getSmsCode(phone);
    }

    @GetAction("getTime.json")
    @ApiMethod(title = "获取服务器当前时间", path = "getTime.json", method = ApiHttpMethod.GET)
    @ApiReturn(type = String.class, description = "当前时间戳")
    public String serviceTime() {
        return basePaasService.serviceTime();
    }

    @GetAction("validateCode.sms")
    @ApiMethod(title = "验证短信验证码", path = "validateCode.sms", method = ApiHttpMethod.GET)
    @ApiParam(name = "mobile", title = "手机号码", type = String.class)
    @ApiParam(name = "smscode", title = "手机验证码", type = String.class)
    @ApiReturn(description = "", type = boolean.class)
    public boolean validateCode(String mobile, String smscode) {
        return basePaasService.validateCode(mobile, smscode);
    }

    @GetAction("getJsToken.json")
    @ApiMethod(title = "换取一个凭证", path = "getJsToken.json", method = ApiHttpMethod.GET)
    @ApiParam(name = "token", title = "旧凭证", type = String.class)
    @ApiReturn(description = "新凭证", type = String.class)
    public String getJsToken(@NotEmpty String token) {
        return basePaasService.getJsToken(token);
    }

    @GetAction("newJsToken.json")
    @ApiMethod(title = "创建一个凭证", path = "newJsToken.json", method = ApiHttpMethod.GET)
    @ApiReturn(description = "凭证", type = String.class)
    @With(ILogin.class)
    public String newJsToken() {
        return basePaasService.newJsToken();
    }
}
